package com.lanou3g.mynewstest.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by dllo on 16/1/21.
 */
public class DownloadImage extends AsyncTask<String, Void, InputStream> {
    private Handler handler;

    public DownloadImage(Handler handler) {
        this.handler = handler;
    }

    //doinbackground 不能做页面刷新
    //onPreExcute--->doinbackground(后台执行,不能刷新UI)-->doPostExecute和publishProgress发布跟新进度发布完了 可以progerssupdata
    @Override
    public InputStream doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();

                return inputStream;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(InputStream inputStream) {
        super.onPostExecute(inputStream);
        Log.d("DownloadImage", "inputStream:" + inputStream);
        Message message  = new Message();
        message.what = 100;


        message.obj = inputStream;

        handler.sendMessage(message);

    }

//    private int calculateInSampleSize(
//            BitmapFactory.Options options, int reqWidth, int reqHeight) {
//        // Raw height and width of image
//        final int height = options.outHeight;
//        final int width = options.outWidth;
//        int inSampleSize = 1;
//
//        if (height > reqHeight || width > reqWidth) {
//
//            final int halfHeight = height / 2;
//            final int halfWidth = width / 2;
//
//            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
//            // height and width larger than the requested height and width.
//            while ((halfHeight / inSampleSize) > reqHeight
//                    && (halfWidth / inSampleSize) > reqWidth) {
//                inSampleSize *= 2;
//            }
//        }
//
//        return inSampleSize;
//    }
//
//    public Bitmap decodeSampledBitmapFromResource(InputStream inputStream,
//                                                  int reqWidth, int reqHeight) {
//
//        ByteArrayOutputStream bos= new ByteArrayOutputStream();
//        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
//
//        Rect rect = new Rect();
//        rect.set(0, 0, 0, 0);
//        // First decode with inJustDecodeBounds=true to check dimensions
//        final BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
////        BitmapFactory.decodeResource(res, resId, options);
//        BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length, options);
////        BitmapFactory.decodeStream(inputStream, rect, options);//null = ret
//        // Calculate inSampleSize
//        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//        //上下左右尺寸
//        // Decode bitmap with inSampleSize set
//        options.inJustDecodeBounds = false;
//        return BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length, options);
////        return BitmapFactory.decodeStream(inputStream, rect, options);
//    }
}
